﻿(function () {
    /* Copyright 2010 Imaginary Development. All rights reserved http://imaginarydevelopment.com */
    var stats = false;
    function DoOnceJquerified() {
        if (typeof ($jq) == 'undefined') {
            if (typeof ($) != 'undefined') {
                $jq = $;
            } else {
                return alert('$jq->jquerify not defined');
            }
        }

        if ($jq('html#facebook').length > 0) {
            alert("Script doesn't work in facebook");
            throw ("Facebook detected");
        }

        if ($jq('a.selected:contains(Galaxy)').length != 1) {
            alert('This script is for your starfleet commander galaxy screen');
            throw "Not on galaxy screen";
        }

        //http://www.tek-tips.com/faqs.cfm?fid=6620
        String.prototype.startsWith = function (str)
        { return (this.match("^" + str) == str) }

        function PlayerLink(span, playerNameText) {
            var url = "http://www.sfcstats.com/Search2.php?Search=Player&Sort=0&Item=";
            if (stats === false)
                url = "http://imaginarydevelopment.com/Sfc/Player/name/";
            $jq(span).append('<a target="_blank" href="' + url + playerNameText + '">Search</a>');
        }
        $jq('table#planets.planets tr.planet td.player span').each(function (index, playerSpan) {

            $jq(playerSpan).contents(':first').filter(function () { return this.nodeName == "#text"; }).each(function (i) {
                if (i == 0) {
                    var playerNameFull = this.textContent;
                    var playerName = playerNameFull.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                    if (playerName.startsWith('#') == false && playerName.charAt(0) != ("("))
                        PlayerLink(playerSpan, playerName);
                }
            });

        });

        $jq('td.debris table.debris_table tr td.resources').each(function (index, td) {
            var text = $jq(td).text();
            var match = text.match('([0-9,]*[0-9])\s*([0-9,]*[0-9])');
            if (match.length == 3) {
                var total = parseInt(match[1].replace(/,/g, ''), 10) + parseInt(match[2].replace(/,/g, ''), 10);
                $jq(td).html($jq(td).html() + '<br />' + (total / 20000));
            }
        });
    };


    var el = document.createElement('div'), b = document.getElementsByTagName('body')[0]; otherlib = false, msg = ''; el.style.position = 'fixed'; el.style.height = '32px'; el.style.width = '220px'; el.style.marginLeft = '-110px'; el.style.top = '0'; el.style.left = '50%'; el.style.padding = '5px 10px 5px 10px'; el.style.zIndex = 1001; el.style.fontSize = '12px'; el.style.color = '#222'; el.style.backgroundColor = '#f99'; if (typeof jQuery != 'undefined') {
        msg = 'SfeDebris Loaded. This page already using jQuery v' + jQuery.fn.jquery;
        DoOnceJquerified();
        return showMsg();
    } else if (typeof $ == 'function') { otherlib = true; } function getScript(url, success) { var script = document.createElement('script'); script.src = url; var head = document.getElementsByTagName('head')[0], done = false; script.onload = script.onreadystatechange = function () { if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done = true; success(); } }; head.appendChild(script); } getScript('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', function () {
        if (typeof jQuery == 'undefined') { msg = 'Sorry, but jQuery wasn\'t able to load'; } else {

            msg = 'SfeDebris Loaded. This page is now jQuerified with v' + jQuery.fn.jquery; if (otherlib) { msg += ' and noConflict(). Use $jq(), not $().'; }
            /* Begin AjaxBookmarklet.js */
            DoOnceJquerified();
            /* End AjaxBookmarklet.js */
        } return showMsg();
    }); function showMsg() { el.innerHTML = msg; b.appendChild(el); window.setTimeout(function () { if (typeof jQuery == 'undefined') { b.removeChild(el); } else { jQuery(el).fadeOut('slow', function () { jQuery(this).remove(); }); if (otherlib) { $jq = jQuery.noConflict(); } } }, 2500); }
})();
